<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="left-header">
        <!-- <img src="@/assets/logo.png" alt="" /> -->
        <i class="el-icon-eleme"></i>
        <span>后台管理系统</span>
      </div>
      <div class="right-header">
        <div class="user-avater">
          <el-avatar>
            <img src="@/assets/img/logo1.jpg" />
          </el-avatar>
        </div>
        <div class="logout-box">
          <a @click="logout" class="logoutBtn">
            <i class="el-icon-switch-button"></i>
            退出系统
          </a>
        </div>
      </div>
    </el-header>
    <!-- 主体区域 -->
    <el-container>
      <!-- 侧边导航栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 动态伸缩导航栏 -->
        <div class="toggle-btn">
          <el-switch v-model="isCollapse" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
        </div>
        <!-- 菜单 -->
        <el-menu background-color="#252b32" text-color="#fff" active-text-color="#409EFF" :collapse="isCollapse" unique-opened collapse-transition router :default-active="activedPath">
          <el-submenu :index="'' + item.id" v-for="item in menuList" :key="item.id">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 一级菜单图标 -->
              <i :class="icons[item.id]"></i>
              <!-- 一级菜单文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/' + subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.initMenuList()
    this.activedPath = sessionStorage.getItem('activedPath')
    console.log(this.activedPath)
  },
  data() {
    return {
      menuList: [],
      icons: {
        125: 'iconfont icon-icon_users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // false表示不水平折叠侧边导航栏
      isCollapse: false,
      // 用于点击二级菜单的时候能够使其高亮
      activedPath: ''
    }
  },
  methods: {
    // 退出按钮响应函数
    logout() {
      // 退出时要清空sessionStorage
      sessionStorage.clear()
      // 重定向回登陆页
      this.$router.push('/login')
    },
    async initMenuList() {
      const { data: res } = await this.$http.get('menus')
      // 获取失败
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 获取成功
      this.menuList = res.data
      // console.log(this.menuList)
    },
    saveNavState(activedPath) {
      sessionStorage.setItem('activedPath', activedPath)
      this.activedPath = activedPath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0px;
  font-size: 20px;
  color: #fff;
  // background-color: #353c3e;
  background-color: #303030;
  > div {
    height: 100%;
    display: flex;
    align-items: center;
    img {
      height: 100%;
    }
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  // background-color: #303641;
  background-color: #303030;
}
.el-main {
  background-color: #e8ecef;
}
.iconfont {
  margin-right: 10px;
}
.el-menu {
  border-right: 0;
}
.toggle-btn {
  text-align: center;
  background-color: #252b32;
}
.logoutBtn {
  font-size: 14px;
  color: #fff;
  cursor: pointer;
}
.el-icon-switch-button {
  color: #409eff;
  margin-right: 5px;
}
.user-avater {
  display: flex;
  margin-right: 30px;
  align-items: center;
}
.left-header {
  padding: 0 10px;
  i {
    color: #f26b3a;
  }
}
</style>
